//
//  CarSourceAddViewController.m
//  阿帕
//
//  Created by mac on 2019/12/30.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "CarSourceAddViewController.h"
#import "CYPlaceholderTextView.h"
#import "CYCityChooseView.h"

@interface CarSourceAddViewController ()<CYChooseRegionDelegate>
{
    NSInteger _selectedRow;
    NSString *_departureCode;//始发地code
    NSString *_throughCode;//途径地code
    NSString *_destinationCode;//目的地code
    NSString *_departure;//始发地
    NSString *_through;//途径地
    NSString *_destination;//目的地
}
@property (strong , nonatomic)CYPlaceholderTextView *textView;

@property (strong , nonatomic)UIButton *confirmButton;

@end

@implementation CarSourceAddViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _selectedRow = 0;
    
    [self baseViewSetup];
    
    if(_addType == 1) {
        [self getCarSourceDetailRequest];
        self.navigationItem.title = @"修改车源信息";
    } else {
        self.navigationItem.title = @"新增车源";
    }
}

- (void)baseViewSetup {
    
    self.view.backgroundColor = BackgroundColor;
    
    NSArray *textTitles = @[@"出发地", @"途径地", @"目的地"];
    for (int i=0; i<textTitles.count; i++) {
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 10+50*i, ScreenW, 45)];
        bgView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:bgView];
        
        UILabel *asteriskLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 10, bgView.height)];
        asteriskLabel.text = @"*";
        asteriskLabel.font = font(14);
        asteriskLabel.textColor = [UIColor redColor];
        [bgView addSubview:asteriskLabel];
        
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, bgView.height)];
        textLabel.text = textTitles[i];
        textLabel.font = font(14);
        textLabel.textColor = Dark_TextColor;
        [bgView addSubview:textLabel];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 100 + i;
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitle:@"请选择" forState:UIControlStateNormal];
        button.titleLabel.font = font(14);
        [button setTitleColor:Dark_TextColor forState:UIControlStateNormal];
        button.frame = CGRectMake(ScreenW-220, 0, 195, bgView.height);
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [button addTarget:self action:@selector(buttonTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:button];
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(button.cy_right + 5, 17, 6, 11)];
        arrow.image = image(@"gcy_icon_my_rArrow");
        [bgView addSubview:arrow];
        
    }
    
    _textView = [[CYPlaceholderTextView alloc] initWithFrame:CGRectMake(0, 160, ScreenW, 100)];
    _textView.text = @"";
    _textView.textColor = Light_TextColor;
    _textView.placeholder = @"请填写详细信息";
    _textView.placeholderColor = Light_TextColor;
    _textView.font = font(14);
    [self.view addSubview:_textView];
    
    [self.view addSubview:self.confirmButton];
}

- (void)getCarSourceDetailRequest {
    WEAKSELF
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], VEHICLE_SOURCE, _code) withParameter:@{ @"token":[UserDefaultUtil valueForKey:USER_TOKEN],} withVC:self netName:@"我的车源详情" success:^(id response, NSString *msg) {
        
        NSArray *btnTitles = @[
            CJStringWithFormat(@"%@", response[@"departure"]),
            CJStringWithFormat(@"%@", response[@"through"]),
            CJStringWithFormat(@"%@", response[@"destination"])
        ];
        for (int i=0; i<btnTitles.count; i++) {
            UIButton *button = [weakSelf.view viewWithTag:i + 100];
            [button setTitle:btnTitles[i] forState:UIControlStateNormal];
        }
        
        _textView.text = CJStringWithFormat(@"%@", response[@"content"]);
        _departureCode = CJStringWithFormat(@"%@", response[@"departureCode"]);
        _throughCode = CJStringWithFormat(@"%@", response[@"throughCode"]);
        _destinationCode = CJStringWithFormat(@"%@", response[@"destinationCode"]);
        _departure = CJStringWithFormat(@"%@", response[@"departure"]);
        _through = CJStringWithFormat(@"%@", response[@"through"]);
        _destination = CJStringWithFormat(@"%@", response[@"destination"]);
        
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

- (void)buttonTypeClick:(UIButton *)button {
    
    _selectedRow = button.tag - 100;
    
    [[CYCityChooseView shared] show];
    [CYCityChooseView shared].pageShowFlag = 1;
    [CYCityChooseView shared].delegate = self;
    
}

#pragma mark - CYChooseRegionDelegate
//- (void)getChooseRegionCode:(NSString *)code andRegionName:(NSString *)name {
//
//}

- (void)getAllInfoProvinceCode:(NSString *)provinceCode provinceName:(NSString *)provinceName cityCode:(NSString *)cityCode cityName:(NSString *)cityName countyCode:(NSString *)countyCode countyName:(NSString *)countyName {
    
    //    DebugLog(@"地区code：%@----地区名称：%@", code, name);
    
    NSString *address = CJStringWithFormat(@"%@%@%@",provinceName,cityName,countyName);
    
    UIButton *button = [self.view viewWithTag:_selectedRow + 100];
    [button setTitle:address forState:UIControlStateNormal];
    
    NSString *code = @"";
    if ([DCSpeedy isBlankString:countyCode] == NO) {
        code = countyCode;
    } else if ([DCSpeedy isBlankString:cityCode] == NO) {
        code = cityCode;
    } else if ([DCSpeedy isBlankString:provinceCode] == NO) {
        code = provinceCode;
    }
    
    if (_selectedRow == 0) {
        //出发地
        _departureCode = code;
        _departure = address;
        
    } else if (_selectedRow == 1) {
        //途径地
        _throughCode = code;
        _through = address;
        
    } else if (_selectedRow == 2) {
        //目的地
        _destinationCode = code;
        _destination = address;
    }
}
//添加
- (void)confirmButtonClick {
    
    WEAKSELF
    
    if ([DCSpeedy isBlankString:_departureCode] == YES || [DCSpeedy isBlankString:_throughCode] == YES || [DCSpeedy isBlankString:_destinationCode] == YES) {
        [XSInfoView showInfo:@"必选信息不能为空" onView:self.view];
        return;
    }
    
    
    
    if (_addType == 1) {
        //修改
        NSDictionary *parmas = @{
            @"departureCode": _departureCode,
            @"throughCode": _throughCode,
            @"destinationCode": _destinationCode,
            @"departure": _departure,
            @"through": _through,
            @"destination": _destination,
            @"content": _textView.text,
            @"code": _code,
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        [HTTPManager putNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], VEHICLE_SOURCE) withParameter:parmas withVC:self netName:@"修改车源信息" success:^(id response, NSString *msg) {
            
            [XSInfoView showInfo:@"修改成功" onView:weakSelf.view];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
        
    } else {
        //新增
        NSDictionary *parmas = @{
            @"departureCode": _departureCode,
            @"throughCode": _throughCode,
            @"destinationCode": _destinationCode,
            @"departure": _departure,
            @"through": _through,
            @"destination": _destination,
            @"content": _textView.text,
            @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], VEHICLE_SOURCE) withParameter:parmas withVC:self netName:@"新增车源信息" success:^(id response, NSString *msg) {
            
            [XSInfoView showInfo:msg onView:weakSelf.view];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
    }
}

- (UIButton *)confirmButton {
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"保存" forState:UIControlStateNormal];
        [_confirmButton setBackgroundColor:Theme_ColorValue];
        cut(_confirmButton, BTN_BORDER_RADIUS);
        [_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmButton.frame = CGRectMake(20, ScreenH - NavHeight - 65, ScreenW-40, 45);
    }
    return _confirmButton;
}

@end
